package Frame.common;

 import java.awt.Color;
 import java.awt.Component;
 
 import javax.swing.JLabel;
 import javax.swing.JTable;
 import javax.swing.table.DefaultTableCellRenderer;

 public class TableBackGround {
     public static void setColumnColor(JTable table) {
         try
        {
             DefaultTableCellRenderer tcr = new DefaultTableCellRenderer(){
                 private static final long serialVersionUID = 1L;
                 public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocus,int row, int column){
                     if(row%2 == 0)
                         setBackground(Color.WHITE);//设置奇数行底色
                     else if(row%2 == 1)
                         setBackground(new Color(247,249,252));//设置偶数行底色
                     return super.getTableCellRendererComponent(table, value,isSelected, hasFocus, row, column);
                 }
             };
             for(int i = 0; i < table.getColumnCount(); i++) {
                 table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
             }
            tcr.setHorizontalAlignment(JLabel.CENTER);
         }
         catch (Exception e){
             e.printStackTrace();
         }
     }

 }